package com.czl.module_storehouse.mvp.model;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.net.RetrofitClient;
import com.czl.base.util.SpHelper;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.model.BaseModel;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.api.ApiService;
import com.czl.module_storehouse.bean.AddStorageBean;
import com.czl.module_storehouse.bean.CardBean;
import com.czl.module_storehouse.bean.PurchaseBean;
import com.czl.module_storehouse.bean.StorageRecordBean;
import com.czl.module_storehouse.bean.StorageTypeModel;
import com.czl.module_storehouse.bean.StoreProjectBean;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/16.
 * PS: Not easy to write code, please indicate.
 */
public class AddWareHouseModel extends BasicDataModel {

    public Observable<HttpResponse<ListBean<SortBean>>> getSortList(RequestBody body) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
//            object.put("companyId",     getCompanyId());
            JSONArray array = new JSONArray();
            array.put(1);
            array.put(2);
            object.put("classStatus", array);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getSortList(RequestBody.create(mediaType, json));
    }


    public Observable<HttpResponse<ListBean<SortBean>>> getSortAllList(RequestBody body) {
        return null;
        /*return Observable.just(mRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .getSortAllList(body))
                .flatMap((Function<Observable<HttpResponse<ListBean<SortBean>>>,
                        ObservableSource<HttpResponse<ListBean<SortBean>>>>)
                        observable -> observable);*/
    }


    public Observable<HttpResponse<ListBean<StorageRecordBean>>> getStorageList(RequestBody body) {
        return null;
       /* return Observable.just(mRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .getStorageList(body))
                .flatMap((Function<Observable<HttpResponse<ListBean<StorageRecordBean>>>,
                        ObservableSource<HttpResponse<ListBean<StorageRecordBean>>>>) observable -> observable);*/
    }


    public Observable<HttpResponse<List<StorageTypeModel>>> getStorageModel() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class)
                .getStorageModel(createBody(object));
    }


    public Observable<HttpResponse<List<CardBean>>> getCardList() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class)
                .getStorehouseCardList(createBody(object));
    }


    public Observable<HttpResponse<Object>> addStorage(AddStorageBean bean) {
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        bean.setStorehouseId(getStorehouseId());
        return RetrofitClient.getInstance().create(ApiService.class)
                .addStorage(createBody(bean));
    }

 /*   public Observable<HttpResponse<ListBean<CompanyBean>>> selectCrmCompanyByTag(RequestBody body) {

        return Observable.just(mRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .selectCrmCompanyByTag(body))
                .flatMap((Function<Observable<HttpResponse<ListBean<CompanyBean>>>,
                        ObservableSource<HttpResponse<ListBean<CompanyBean>>>>) observable -> observable);
    }*/


    public Observable<HttpResponse<ListBean<PurchaseBean>>> appgetRukuPurhchaseCardList() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", getManageCompanyId());
            object.put("storehouseId", getStorehouseId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class)
                .appgetRukuPurhchaseCardList(createBody(object));
    }


    public Observable<HttpResponse<ListBean<StoreProjectBean>>> selectProjectInStoreHouse(RequestBody body) {
        return null;
       /* return Observable.just(mRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .selectProjectInStoreHouse(body))
                .flatMap((Function<Observable<HttpResponse<ListBean<StoreProjectBean>>>,
                        ObservableSource<HttpResponse<ListBean<StoreProjectBean>>>>) observable -> observable);*/
    }


    public Observable<HttpResponse<ListBean<SortBean>>> mateSortList(List<SortBean> list) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
            object.put("sortList", new Gson().toJson(list));
            JSONArray array = new JSONArray();
            array.put(1);
            array.put(2);
            object.put("classStatus", array);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .mateSortList(RequestBody.create(mediaType, json));
    }
}
